package com.hzit.controller;

import com.hzit.common.entity.PageResult;
import com.hzit.common.entity.R;
import com.hzit.entity.Classes;
import com.hzit.entity.Student;
import com.hzit.service.IClassesService;
import com.hzit.service.IStudentService;
import com.hzit.vo.StudentVo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import javafx.beans.property.IntegerProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wf
 * @since 2023-08-16
 */
@Controller
@RequestMapping("/student")
public class StudentController {
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IClassesService classesService;
	//1. 查询学生
	@GetMapping("/list")
	public String findAll(Model model){
		// List<StudentVo>  studentVos = studentService.findAll2();   // 使用原始的redis的处理
		List<StudentVo>  studentVos = studentService.findAll3();      // 使用springCache处理缓存
		model.addAttribute("list",studentVos);
		//3.3 放到班级列表
		model.addAttribute("classes",classesService.list());
		return "list2";
	}

	//2. 分页查询所有学生
	@GetMapping("/findByPage")
	public String findByPage(@RequestParam(required = false,defaultValue = "1") Integer page,
	                         @RequestParam(required = false,defaultValue = "5") Integer pageSize,
	                         Model model){
		//2.1 进行分页查询
		PageResult<StudentVo> svpr = studentService.findByPage(page,pageSize);
		//2.2 放到model中
		model.addAttribute("pr",svpr);
		//2.3 放到班级列表
		model.addAttribute("classes",classesService.list());
		//2.4 返回视图
		return "list";
	}

	//3. 分页带条件查询
	@RequestMapping("/search")
	// @PreAuthorize("hasAuthority('student:list')")
	public String search(@RequestParam(required = false,defaultValue = "1") Integer page,
	                     @RequestParam(required = false,defaultValue = "5") Integer pageSize,
	                     StudentVo vo,
	                     Model model){
		List<String> authorities = SecurityContextHolder.getContext().getAuthentication()
				.getAuthorities()
				.stream()
				.map(GrantedAuthority::getAuthority).collect(Collectors.toList());
		System.out.println("authorities = " + authorities);
		//3.1 进行分页查询
		PageResult<StudentVo> svpr = studentService.search(page,pageSize,vo);
		//3.2 放到model中
		model.addAttribute("pr",svpr);
		//3.3 放到班级列表
		model.addAttribute("classes",classesService.list());
		model.addAttribute("vo",vo);
		model.addAttribute("authorities",authorities);
		return "list";
	}

	//4. 根据sid来删除学生
	// @PreAuthorize("hasAuthority('student:delete')")
	@DeleteMapping("/delete/{id}")
	public String delete(@PathVariable("id") Integer sid, Model model){
		studentService.removeById(sid);
		model.addAttribute("permission","student:delete");
		return "redirect:/student/search";
	}

	//5. 添加学生
	@PostMapping("/add")
	// @PreAuthorize("hasAuthority('student:create')")
	public String add(StudentVo vo,Model model){
		Student student = new Student();
		BeanUtils.copyProperties(vo,student);
		studentService.save(student);
		model.addAttribute("permission","student:create");
		return "redirect:/student/search";
	}

	//5. 添加学生
	@PutMapping("/update")
	// @PreAuthorize("hasAuthority('student:update')")
	public String update(StudentVo vo, Model model){
		Student student = new Student();
		BeanUtils.copyProperties(vo,student);
		studentService.update(student);
		model.addAttribute("permission","student:update");
		return "redirect:/student/search";
	}

	//6. 删除学生
	@GetMapping("/delStud")
	public String delStud(Integer id){
		studentService.delete(id);
		return "redirect:/student/list";
	}
}
